package main
	
import (
	"sanguo/protocol/cs/proto_def"
	"fmt"
	"os"
	"github.com/sniperHW/kendynet"
	"github.com/sniperHW/kendynet/golog"
	//"strings"
)

//产生协议注册文件
func gen_lua_id(out_path string) {

	f,err := os.OpenFile(out_path,os.O_RDWR,os.ModePerm)
	if err != nil {
		if os.IsNotExist(err) {
			f,err = os.Create(out_path)
			if err != nil {
				kendynet.Errorf("create %s failed:%s",out_path,err.Error())
				return
			}
		} else {
			kendynet.Errorf("open %s failed:%s",out_path,err.Error())			
			return
		}
	}

	err = os.Truncate(out_path,0)

	if err != nil {
		kendynet.Errorf("Truncate %s failed:%s",out_path,err.Error())			
		return
	}

	str := `
return {
`

	for id,v := range(proto_def.CS_message) {
		str = str + fmt.Sprintf(`	[%d] = {name='%s',desc='%s'},`, id + 1,v.Name,v.Desc) + "\n"	
	}

	next_id_begin := 5000

	for id,v := range(proto_def.Room_message) {
		str = str + fmt.Sprintf(`	[%d] = {name='%s',desc='%s'},`, next_id_begin + id + 1,v.Name,v.Desc) + "\n"	
	}

	str = str + "}\n"



	_,err = f.WriteString(str)

	//fmt.Printf(str)

	if nil != err {
		kendynet.Errorf("%s Write error:%s\n",out_path,err.Error())						
	} else {
		kendynet.Infof("%s Write ok\n",out_path)		
	}

	f.Close()

}


func main() {
	outLogger := golog.NewOutputLogger("log","proto_gen_cs",1024*1024*1000)
	kendynet.InitLogger(outLogger)

	os.MkdirAll("../message",os.ModePerm)
	gen_lua_id("../messageID.lua")
	kendynet.Infoln("------------------------------------------")	
	kendynet.Infoln("cs gen_proto_lua ok!")	
}